var app=new Vue({
    el: '#app',
    data:{
        taskList: null,
        searchTask:{
            total:0,
            pageSize:10,
            pageIndex:1,
            startEndDate: ""
        },
        dicts:{
            taskStatus: [],
            taskType: []
        }
    },
    methods:{
        init: function(){
            var _this=this;
            setTimeout(function(){
                _this.findAll();
                _this.initDict();
            },100);
        },
        pauseJob: function(index,row){
            var _this=this;
            top.app.$confirm('此操作将暂停该任务, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                jsmile.http.sendJson({
                    url:jsmile.url+"/sys/task/pauseJob",
                    data:{"id":row.id,"jobName":row.jobName,"jobGroup":row.jobGroup}
                }).done(function(res){
                    jsmile.msg.success(res.resMsg?res.resMsg:"暂停任务成功!");
                    _this.query();
                }).fail(function(res){
                    jsmile.msg.error(res.resMsg?res.resMsg:"暂停任务失败!");
                });
            }).catch(function() {
                jsmile.msg.info('已取消暂停该任务');
            });
        },
        resumeJob: function(index,row){
            var _this=this;
            top.app.$confirm('此操作重新启动该任务, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                jsmile.http.sendJson({
                    url:jsmile.url+"/sys/task/resumeJob",
                    data:{"id":row.id,"jobName":row.jobName,"jobGroup":row.jobGroup}
                }).done(function(res){
                    jsmile.msg.success(res.resMsg?res.resMsg:"启动任务成功!");
                    _this.query();
                }).fail(function(res){
                    jsmile.msg.error(res.resMsg?res.resMsg:"启动任务失败!");
                });
            }).catch(function() {
                jsmile.msg.info('已取消启动任务');
            });
        },
        runJob: function(index,row){
            var _this=this;
            top.app.$confirm('此操作将立即执行一次该任务, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                jsmile.http.sendJson({
                    url:jsmile.url+"/sys/task/runJob",
                    data:{"id":row.id,"jobName":row.jobName,"jobGroup":row.jobGroup}
                }).done(function(res){
                    jsmile.msg.success(res.resMsg?res.resMsg:"立即执行该任务成功!");
                    _this.query();
                }).fail(function(res){
                    jsmile.msg.error(res.resMsg?res.resMsg:"立即执行该任务失败!");
                });
            }).catch(function() {
                jsmile.msg.info('已取消立即执行该任务');
            });
        },
        addTask: function(){
            top.layer.open(jsmile.dialog.init({
                title: "新增任务",
                content: "sys/task/add.html"
            }));
        },
        editTask: function(index,row){
            var _this=this;
            top.layer.open(jsmile.dialog.init({
                title: "修改任务信息",
                content: "sys/task/edit.html",
                success: function(layero,index){
                    var iframeW=layero.find('iframe')[0];
                    iframeW.contentWindow.app.task=row;
                }
            }));
        },
        deleteTask: function(index,row){
            var _this=this;
            top.app.$confirm('此操作将永久删除该任务, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function() {
                jsmile.http.sendJson({
                    url:jsmile.url+"/sys/task/delete",
                    data:{"id":row.id,"jobName":row.jobName,"jobGroup":row.jobGroup}
                }).done(function(res){
                    jsmile.msg.success(res.resMsg?res.resMsg:"删除任务成功!");
                    _this.query();
                }).fail(function(res){
                    jsmile.msg.error(res.resMsg?res.resMsg:"删除任务失败!");
                });
            }).catch(function() {
                jsmile.msg.info('已取消删除');
            });
        },
        query: function(){
            this.searchTask.pageIndex=1;
            this.findAll();
        },
        /**
         * 初始化数据字典
         */
        initDict: function(){
            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/dict/findDictByCode",
                data:{"dictCode":"TK01"}
            }).done(function(res){
                res.resData.splice(0,0,{"dictName":"全部","dictValue":"-1"});
                _this.dicts.taskType=res.resData;
            });
            var s=jsmile.copy([],jsmile.dict.zt);
            s.unshift({"dictName":"全部状态","dictValue":"-1"});
            _this.dicts.status=s;
        },
        goHome: function(url){
            top.$('#home').attr("src","homePage/home.html");
        },
        handleSizeChange: function(val) {
            this.searchTask.pageSize=val;
            this.findAll();
        },
        handleCurrentChange: function(val) {
            this.searchTask.pageIndex=val;
            this.findAll();
        },
        /*
         * 获取所有数据
         */
        findAll: function(){
            if(!this.searchTask.startEndDate){
                this.searchTask.startEndDate="";
            }else{
                this.searchTask.startTime=this.searchTask.startEndDate[0];
                this.searchTask.endTime=this.searchTask.startEndDate[1];
            }

            var _this=this;
            jsmile.http.sendJson({
                url:jsmile.url+"/sys/task/findAll",
                data:_this.searchTask
            }).done(function(res){
                _this.taskList=res.resData.list;
                _this.searchTask.total=res.resData.total;
            }).fail(function(res){
                jsmile.msg.error(res.resMsg?res.resMsg:"获取任务列表失败!");
            });
        }
    }
});
$(function(){
    init();
    function init(){
        app.init();
    }
});